using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.WowAddonStudio.Lua.Parser;

namespace Microsoft.WowAddonStudio.Lua.Tests.Parser
{
    public static class Extensions
    {
        public static Scanner SetSource(this Scanner scanner, string source)
        {
            if (scanner == null)
                throw new ArgumentNullException("scanner");
            if (source == null)
                throw new ArgumentNullException("source");

            scanner.SetSource(source, 0);

            return scanner;
        }

        public static IEnumerable<ParsedToken> GetTokens(this Scanner scanner)
        {
            if (scanner == null)
                throw new ArgumentNullException("scanner");

            Tokens token;

            do
            {
                token = (Tokens)scanner.yylex();
                if (token != Tokens.EOF)
                    yield return new ParsedToken(token, scanner.yylloc);
            } while (token > Tokens.EOF);
        }

        public static string Concatenate<T>(this IEnumerable<T> collection, string delimiter)
        {
            if (collection == null)
                throw new ArgumentNullException("collection");
            
            var stringBuilder = new StringBuilder();

            foreach (var item in collection)
            {
                if (stringBuilder.Length > 0)
                    stringBuilder.Append(delimiter);

                stringBuilder.Append(item);
            }

            return stringBuilder.ToString();
        }

    }
}